package com.mcxx.modules.extremepoverty.controller;

import com.alibaba.fastjson.JSON;
import com.mcxx.modules.extremepoverty.service.ExOrganizationService;
import com.mcxx.modules.miniSalvation.family.dto.OrgSupperCheckDTO;
import com.mcxx.modules.miniSalvation.family.dto.OrganizationDTO;
import com.mcxx.modules.miniSalvation.family.entity.OrganizationEntity;
import com.mcxx.modules.miniSalvation.family.param.OrganizationParam;
import com.mcxx.modules.miniSalvation.family.service.read.OrganizationReadService;
import com.mcxx.modules.miniSalvation.family.service.write.OrganizationWriteService;
import com.mcxx.result.BaseResult;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.*;
import com.mcxx.validate.group.Update;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.validate.annotation.V;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Slf4j
@Validated
@Api("特困供养机构控制类")
@RestService({"extremePoverty/supportOrg/request/v2"})
public class ExSupportOrgController extends DefaultRestService {

    @Autowired
    private OrganizationReadService organizationReadService;
    @Autowired
    private OrganizationWriteService organizationWriteService;
    @Autowired
    private ExOrganizationService exOrganizationService;

    @PostMapping("queryOrganizationList")
    @ApiOperation("查看供养机构数据列表")
    public void queryOrganizationList(OrganizationParam param, PageParam pageParam){
        UserAccountVo userAccountVo = getCurUserAccount();
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        Page page = organizationReadService.queryOrganizationList(param,pageParam,userAccountVo);
        this.setPageBean(page);
    }
    @PostMapping("addOrgName")
    @ApiOperation("新增供养机构名称")
    public void addOrgName(OrganizationDTO dto){
        log.info("新增供养机构名称，参数是：{}", dto);
        int rstFlag = exOrganizationService.addOrgName(dto,getCurUserAccount());
        setData(true);
        log.info("新增供养机构名称，结果是：{}", rstFlag);
    }
    @PostMapping("modifyOrgNameById")
    @ApiOperation("修改供养机构名称")
    public void modifyOrgNameById(OrganizationDTO dto){
        log.info("修改供养机构名称，参数是：{}", dto);
        int rstFlag = exOrganizationService.addOrgName(dto,getCurUserAccount());
        setData(true);
        log.info("修改供养机构名称，结果是：{}", rstFlag);
    }

    @GetMapping("queryOrganizationByID")
    @ApiOperation("根据id查询供养机构")
    public void queryOrganizationById(@V({"notnull"}) String id){
        OrganizationEntity entity = organizationReadService.getById(id);
        BaseResult<OrganizationEntity> rstMsg = new BaseResult<>();
        rstMsg.setCode("1");
        rstMsg.setMsg("操作成功");

        rstMsg.setData(entity);
        setData(rstMsg);
    }
    @PostMapping("deleteOrganizationById")
    @ApiOperation("删除供养机构信息")
    public void deleteOrganizationById(@V({"notnull"}) String id){
        BaseResult<Boolean> rstMsg  = new BaseResult<>();
        rstMsg.setData(true);
        rstMsg.setMsg("删除成功");
        int rst = organizationWriteService.deleteById(id);
        if(rst <= 0){
            rstMsg.setData(false);
            rstMsg.setMsg("操作异常，删除失败");
        }
        setData("data", rstMsg);
    }

    @PostMapping("enableOrganization")
    @ApiOperation("修改供养机构状态")
    public void enableOrganization(OrgSupperCheckDTO dto){
        log.info("修改供养机构状态，参数是：{}", dto);
        if (CommonUtil.invalidArgs(dto.getIds())) {
            setError(FailureCode.ERR_10000.getMessage());
            return;
        }
        BaseResult<String> res = exOrganizationService.updateOrgStateById(dto);
        setData(res);
    }

    @PostMapping("modifyStatus")
    @ApiOperation(value="修改供养机构状态")
    public void modifyStatus(@V({"notnull"}) String id, @V({"notnull"})String status){
        log.info("修改供养机构状态,参数：id:{}, status:{}", id, status);
        exOrganizationService.modifyStatus(id, status);
        log.info("修改供养机构状态,结果:{}", true);
    }
    @PostMapping("updateOrgById")
    @ApiOperation("修改供养机构信息")
    public void updateOrgById(@Validated(Update.class) OrganizationDTO dto){
        log.info("编辑供养机构信息，参数是：{}", dto);
        int rstFlag = exOrganizationService.updateOrgById(dto, getCurUserAccount());
        setData(true);
        log.info("编辑供养机构信息，结果是：{}", rstFlag);
    }

    @PostMapping("queryByOrderAndOrgId")
    @ApiOperation("查询机构成员列表")
    public void queryByOrderAndOrgId(OrgSupperCheckDTO dto, PageParam pageParam){
        Page page = exOrganizationService.queryByOrderAndOrgId(dto,pageParam);
        this.setPageBean(page);
    }


    @PostMapping("downLoadOrganizationList")
    @ApiOperation("供养机构导出")
    public void downLoadOrganizationList(HttpServletResponse response, @RequestBody OrganizationParam param)throws IOException {
        log.info("供养机构导出开始 参数{}", JSON.toJSONString(param));
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        ExcelInfo excelInfo = exOrganizationService.exportOrganizationList(param,getCurUserAccount());
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
        log.info("供养机构导出结束 参数{}", excelInfo);
    }
}
